#!/usr/bin/env sh

set -ex

function build_x264()
{
    echo "===== Building X264 on $1 ====="

    if [ -d extra/x264-$1 ]; then
        rm -rf extra/x264-$1
    fi
    cp -R extra/x264 extra/x264-$1
    cd extra/x264-$1

    if [[ ""$1 == "arm64" ]]; then
        X264_HOST="aarch64-apple-darwin"
    else
        X264_HOST=$1"-apple-darwin"
    fi

    CC=clang ./configure \
        --host=$X264_HOST \
        --enable-shared \
        --enable-pic \
        --enable-strip \
        --disable-asm \
        --extra-cflags="-target $1-apple-macos -mmacosx-version-min=10.13" \
        --extra-ldflags="-target $1-apple-macos -mmacosx-version-min=10.13" \
        --prefix=$REPO/build/x264-$1
    make -j8
    make install
    cd -

    echo "===== Building X264 on $1 end ====="
}

function build_fdkaac()
{
    echo "===== Building FDKAAC on $1 ====="

    if [ -d extra/fdk-aac-$1 ]; then
        rm -rf extra/fdk-aac-$1
    fi
    cp -R extra/fdk-aac extra/fdk-aac-$1
    cd extra/fdk-aac-$1

    if [ ""$1 == "arm64" ]; then
        FDK_HOST="--host=aarch64-apple-darwin"
    else
        FDK_HOST=""
    fi

    ./configure \
    CC=clang CXX=clang++ \
    CFLAGS="-mmacosx-version-min=10.13 -target $1-apple-macos" \
    CXXFLAGS="-mmacosx-version-min=10.13 -target $1-apple-macos" \
    $FDK_HOST \
    --enable-static=no \
    --enable-shared=yes \
    --prefix=$REPO/build/fdk-aac-$1 \

    make -j8
    make install
    cd -

    echo "===== Building FDKAAC on $1 end ====="
}

function install_dav1d_build_tool()
{
    which meson || brew install meson
    which ninja || brew install ninja
    which nasm || brew install nasm
	which pkg-config || brew install pkg-config
}

function build_dav1d()
{
    echo "===== Building dav1d on $1 ====="

    install_dav1d_build_tool
    DAV1D_BUILD_DIR=`realpath extra/dav1d/build_$1`
    [ -d $DAV1D_BUILD_DIR ] && rm -rf $DAV1D_BUILD_DIR || echo
    mkdir $DAV1D_BUILD_DIR
    cd $DAV1D_BUILD_DIR

    if [ ""$1 == "arm64" ]; then
        cat > arm64.cfg <<- EOS
[binaries]
c = 'clang'
cpp = 'clang++'
ar = 'ar'
strip = 'strip'
[host_machine]
system = 'darwin'
cpu_family = 'aarch64'
cpu = 'arm64'
endian = 'little'
EOS
        DAV1D_C_ARGS="-mmacosx-version-min=10.13 -target arm64-apple-macos"
        DAV1D_C_LINK_ARGS="-target arm64-apple-macos"
        DAV1D_CROSS_FILE="--cross-file=arm64.cfg"
    else
        cat > x86_64.cfg <<- EOS
[binaries]
c = 'clang'
cpp = 'clang++'
ar = 'ar'
strip = 'strip'
[host_machine]
system = 'darwin'
cpu_family = 'x86_64'
cpu = 'x86_64'
endian = 'little'
EOS
        DAV1D_C_ARGS="-mmacosx-version-min=10.13 -target x86_64-apple-macos"
        DAV1D_C_LINK_ARGS="-target x86_64-apple-macos"
        DAV1D_CROSS_FILE="--cross-file=x86_64.cfg"
    fi

    if [[ `uname -m` == $1 ]]; then
        DAV1D_CROSS_BUILD=""
    else
        DAV1D_CROSS_BUILD=$DAV1D_CROSS_FILE
    fi

    meson \
    --buildtype release \
    --default-library shared \
    -Dc_args="$DAV1D_C_ARGS" \
    -Dc_link_args="$DAV1D_C_LINK_ARGS" \
    --prefix $REPO/build/dav1d-$1 \
    $DAV1D_CROSS_BUILD \
    ..

    ninja
    ninja install
    cd -

    # delete symbolic files
    cd build/dav1d-$1/lib/
    for lib in `find *.dylib -type l`; do
        rm $lib
    done

    # change install_name with @rpath prefix
    mv *.dylib libdav1d.dylib
    install_name_tool -id @rpath/libdav1d.dylib libdav1d.dylib

    cd -

    echo "===== Building dav1d on $1 end ====="
}

function install() {
    cd $REPO

    FIRST_ARCH=$(echo $1 | grep -oE "^[^;]+")
    ARCH_LIST=$(echo $1 | grep -Eo "[^;]+")
    HAS_ARM64=$(echo $ARCH_LIST | grep -o "arm64" || true)
    HAS_X86_64=$(echo $ARCH_LIST | grep -o "x86_64" || true)

    if [[ -n $HAS_ARM64 && -n $HAS_X86_64 ]]; then
        for libfile in `find build/dav1d-x86_64/lib/*.dylib -type f`; do
            libname="${libfile##*/}"
            # create FAT dynamic library
            lipo build/dav1d-{arm64,x86_64}/lib/$libname -create -output buildscripts/build/thirdparty/mac/lib/$libname
        done

        # libtool -static -o buildscripts/build/thirdparty/mac/lib/libfdk-aac.a build/fdk-aac-{arm64,x86_64}/lib/libfdk-aac.a
        # libtool -static -o buildscripts/build/thirdparty/mac/lib/libx264.a build/x264-{arm64,x86_64}/lib/libx264.a
    elif [[ -n $HAS_X86_64 ]]; then
        cp build/dav1d-x86_64/lib/*.dylib buildscripts/build/thirdparty/mac/lib/
        # cp build/fdk-aac-x86_64/lib/*.a buildscripts/build/thirdparty/mac/lib/
        # cp build/x264-x86_64/lib/*.a buildscripts/build/thirdparty/mac/lib/
    elif [[ -n $HAS_ARM64 ]]; then
        cp build/dav1d-arm64/lib/*.dylib buildscripts/build/thirdparty/mac/lib/
        # cp build/fdk-aac-arm64/lib/*.a buildscripts/build/thirdparty/mac/lib/
        # cp build/x264-arm64/lib/*.a buildscripts/build/thirdparty/mac/lib/
    else
        echo "Error: none of library"
        return -1
    fi

    cp -vR build/dav1d-$FIRST_ARCH/include/* buildscripts/build/thirdparty/mac/include
    # cp -vR build/fdk-aac-$FIRST_ARCH/include/* buildscripts/build/thirdparty/mac/include
    # cp -vR build/x264-$FIRST_ARCH/include/* buildscripts/build/thirdparty/mac/include
}

REPO=$(git rev-parse --show-toplevel)

if [[ -z $1 ]]; then
    ARCH_LIST=`uname -m`  # use native arch by default
elif [[ $1 == "all" ]]; then
    ARCH_LIST="x86_64;arm64"
else
    ARCH_LIST=$1
fi

for ARCH in $(echo $ARCH_LIST | grep -Eo "[^;]+"); do
    cd $REPO

    # echo "===== Building x264 on $ARCH begin ====="
    # rm -rf build/x264-$ARCH
    # build_x264 $ARCH
    # echo "===== Building x264 on $ARCH success ====="

    # echo "===== Building libfdk-aac on $ARCH begin ====="
    # rm -rf build/fdkaac-$ARCH
    # build_fdkaac $ARCH
    # echo "===== Building libfdk-aac on $ARCH success ====="

    echo "===== Building dav1d on $ARCH begin ====="
    rm -rf build/dav1d-$ARCH
    build_dav1d $ARCH
    echo "===== Building dav1d on $ARCH success ====="
done

install $ARCH_LIST
